#!/usr/bin/env python
# coding: utf-8

import os
import gtk, gtk.glade
import gobject
import cPickle as pickle
from xml.etree.ElementTree import ElementTree

from wordpath import *
from sound import read, play
from parse import Parse
# from reciterecord import ReciteRecord
# from widgets import show_info

(
    COLUMN_EN,
    COLUMN_CN,
) = range(2)

class Recite():
    def __init__(self, book, wTree):
        self.book = book
        print self.book
        self.wTree = wTree

        # 单词显示列表
        self.treeview_words = self.wTree.get_widget("treeview_words_first")
        self.model = gtk.ListStore(COLUMN, COLUMN)

        self.treeview_words.set_model(self.model)

        # 添加两标题列
        self.__add_title_column("单词", COLUMN_EN)
        self.__add_title_column("中文解释", COLUMN_CN)

        if self.book:
            self.__create_words_list()

        # 单击时可以发音
        selection = self.treeview_words.get_selection()
        selection.set_mode(gtk.SELECTION_BROWSE)
        selection.connect("changed", self.selection_changed)


#         # 设置单词选择数目
#         spinbutton_words = self.wTree.get_widget("spinbutton_words")
#         spinbutton_words.set_adjustment(gtk.Adjustment(25, 5, 100, 1, 1, 1))
#         spinbutton_words.connect("value-changed", self.on_spinbutton_words_change_value)

#         # 确定按钮
#         button_ok = self.wTree.get_widget("button_ok")
#         button_ok.connect("clicked", self.on_button_ok_clicked)

    # 添加列标题
    def __add_title_column(self, title, column_id):
        column = gtk.TreeViewColumn(title, gtk.CellRendererText(), text = column_id)
        column.set_sort_column_id(column_id)
        self.treeview_words.append_column(column)


    # 创建单词列表
    def __create_words_list(self):
        # 添加
        pa = Parse(self.book)          # 定义一个解析对象
        words_list = pa.mean_summary()
        # print words_list
        for i in range(100):
            li_iter = self.model.append()
            self.model.set(li_iter,
                      COLUMN_EN, words_list[i]['name'],
                      COLUMN_CN, words_list[i]['meaning'])
    
    def selection_changed(self, widget):
        """单击词条时触发
        1、发音
        2、显示详细的解释
        """
        # 发音
        (tree_model, tree_iter) = widget.get_selected()
        if tree_iter:
            select_word = tree_model.get_value(tree_iter, 0)
            read(select_word)

        meaning_detail = self.wTree.get_widget("textview_detail")

        pa = Parse(self.book)
        word_dic = pa.mean_detail(select_word)
        if word_dic['sentence']:
            sen_display = "\n例句与用法:"
            i = 1                       # 例句标号
            for item in word_dic['sentence']:
                sen_display +=  '\n\n' + str(i) + '. ' + item[0] + '\n    ' +  item[1]
                i += 1
        else:
            sen_display = ''
            
        detial = gtk.TextBuffer()
        detial.set_text(
            word_dic['name'] \
            + "\n" + word_dic['pronounce'] \
            + "\n" + word_dic['meaning']
            + "\n" \
            + sen_display)

        meaning_detail.set_buffer(detial)
        
#     # 单词数改变时触发
#     def on_spinbutton_words_change_value(self, widget, data = None):
#         self.model.clear()
#         self.create_model(widget.get_value())

#     # 单击OK时触发
#     def on_button_ok_clicked(self, widget, data = None):
#         if self.rr:
#             if self.rr.num >= 5:
#                 self.wordtest.create_test(self.rr)
#                 self.treeview_words.hide()
#                 self.status.set_markup('<span size="xx-large">单词初记-测试</span>')
#                 self.wordtest.entry.grab_focus()
#                 self.wordtest.show()
#             elif self.rr.num < 5:
#                 show_info("单词少于5个时不能进行背诵")
#         else:
#             show_info("还没有选书呢！请选书！")


if __name__ == "__main__":
    wTree = gtk.glade.XML(GLADE_PATH + "first_test.glade")

    window_first = wTree.get_widget("window_first")
    window_first.connect('destroy', lambda *w: gtk.main_quit())

    Recite(BOOK_PATH + "beginner/grade_1.xml", wTree)

    gtk.main()
